﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TERP.WaterClient.Common
{
	public static class DataSetHelper
	{
		private static DataSet ds;
		private static bool ColumnEqual(object A, object B)
		{

			// Compares two values to see if they are equal. Also compares DBNULL.Value.
			// Note: If your DataTable contains object fields, then you must extend this
			// function to handle them in a meaningful way if you intend to group on them.

			if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
				return true;
			if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
				return false;
			return (A.Equals(B));  // value type standard comparison
		}

		public static DataTable SelectDistinct(string TableName,DataTable SourceTable, string FieldName, string FieldName1)
		{
			DataTable dt = new DataTable(TableName);
			ds = new DataSet();
			dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
			dt.Columns.Add(FieldName1, SourceTable.Columns[FieldName].DataType);
			object LastValue = null;
			foreach (DataRow dr in SourceTable.Select("", FieldName))
			{
				object objValue = null;
				if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
				{
					LastValue = dr[FieldName];
					objValue = dr[FieldName1];
					dt.Rows.Add(new object[] { LastValue, objValue });
				}
			}
			if (ds != null)
				ds.Tables.Add(dt);
			return dt;
		}

		public static DataTable SelectDistinct(string TableName
			, DataTable SourceTable
			, string FieldName
			, string FieldName1
			, string FieldName2
			)
		{
			ds = new DataSet();
			DataTable dt = new DataTable(TableName);
			dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
			dt.Columns.Add(FieldName1, SourceTable.Columns[FieldName].DataType);
			dt.Columns.Add(FieldName2, SourceTable.Columns[FieldName].DataType);
			object LastValue = null;
			foreach (DataRow dr in SourceTable.Select("", FieldName))
			{
				object objValue = null;
				object objValue1 = null;
				if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
				{
					LastValue = dr[FieldName];
					objValue = dr[FieldName1];
					objValue1 = dr[FieldName2];
					dt.Rows.Add(new object[] { LastValue, objValue,objValue1 });
				}
			}
			if (ds != null)
				ds.Tables.Add(dt);
			return dt;
		}

		public static DataTable Select2Distinct(string TableName
			, DataTable SourceTable
			, string FieldName
			, string FieldName1
			, string FieldName2
			)
		{
			ds = new DataSet();
			DataTable dt = new DataTable(TableName);
			dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
			dt.Columns.Add(FieldName1, SourceTable.Columns[FieldName].DataType);
			dt.Columns.Add(FieldName2, SourceTable.Columns[FieldName].DataType);
			object LastValue1 = null;
			object LastValue2 = null;
			foreach (DataRow dr in SourceTable.Select("", FieldName))
			{
				object objValue = null;
				if ((LastValue1 == null && LastValue2==null) || !(ColumnEqual(LastValue1, dr[FieldName])&& ColumnEqual(LastValue2,dr[FieldName1])))
				{
					LastValue1 = dr[FieldName];
					LastValue2= dr[FieldName1];
					objValue = dr[FieldName2];
					dt.Rows.Add(new object[] { LastValue1, LastValue2, objValue });
				}
			}
			if (ds != null)
				ds.Tables.Add(dt);
			return dt;
		}

		public static DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
		{
			ds = new DataSet();
			DataTable dt = new DataTable(TableName);
			dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);

			object LastValue = null;
			foreach (DataRow dr in SourceTable.Select("", FieldName))
			{
				if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
				{
					LastValue = dr[FieldName];
					dt.Rows.Add(new object[] { LastValue });
				}
			}
			if (ds != null)
				ds.Tables.Add(dt);
			return dt;
		}

		public static DataTable SelectDistinct(
			string TableName
			, DataTable SourceTable
			, string DistinctFieldName
			, List<string> lstFieldReturnName)
		{
			ds = new DataSet();
			DataTable dt = new DataTable(TableName);
			dt.Columns.Add(DistinctFieldName, SourceTable.Columns[DistinctFieldName].DataType);
			foreach (string item in lstFieldReturnName)
			{
				dt.Columns.Add(item, SourceTable.Columns[item].DataType);
			}

			object LastValue = null;
			foreach (DataRow dr in SourceTable.Select("", DistinctFieldName))
			{
				if (LastValue == null || !(ColumnEqual(LastValue, dr[DistinctFieldName])))
				{
					LastValue = dr[DistinctFieldName];
					List<object> lstObj = new List<object>();
					lstObj.Add(LastValue);

					foreach (string item in lstFieldReturnName)
					{
						lstObj.Add(dr[item]);
					}
					object[] arrObj = lstObj.ToArray();

					dt.Rows.Add(arrObj);
				}
			}
			if (ds != null)
				ds.Tables.Add(dt);
			return dt;
		}
	}

}
